<template>
  <vol-box
    :lazy="true"
    v-model="model"
    title="领用单明细"
    :height="400"
    :width="1200"
    :padding="5"
    :onModelClose="onModelClose"
  >
    <produceUseMx ref="produceUseMx"></produceUseMx>
    <template #footer>
      <div>
        <el-button type="primary" size="small" @click="detailOnSelect"
          >确认</el-button
        >
      </div>
    </template>
  </vol-box>
</template>
      
<script>
import VolBox from "@/components/basic/VolBox.vue";
import produceUseMx from "@/views/produceinfo/produce_use_mx/q_produce_use_mx.vue";
export default {
  components: {
    "vol-box": VolBox,
    produceUseMx,
  },
  data() {
    return {
      model: false,
    };
  },
  methods: {
    async onModelClose() {
      this.$refs.produceUseMx.searchFormFields.领用单号 = null;
      this.$refs.produceUseMx.searchFormFields.配件库位 = null;
      this.$refs.produceUseMx.searchFormFields.配件编码 = null;
      this.$refs.produceUseMx.searchFormFields.配件名称 = null;
      this.$refs.produceUseMx.searchFormFields.生产状态 = null;
    },
    async open(Produce_use,WHSE_jg) {
      this.model = true;
      let setIntervalTiem = setInterval(() => {
        if (this.$refs.produceUseMx) {
          clearInterval(setIntervalTiem);
          this.$refs.produceUseMx.searchFormFields.领用单号 = Produce_use;
          this.$refs.produceUseMx.searchFormFields.配件库位 = WHSE_jg;
          this.$refs.produceUseMx.searchFormFields.生产状态 = "1";
          // if(WHSE_jg){
            //this.$refs.produceUseMx.searchFormOptions.forEach(item=>{
              //if(item.field=="配件库位"){
                //item.disabled=true
              //} 
            //})
          //}
          this.$refs.produceUseMx.$refs.grid.search();
        }
      }, 100);
    },
    async detailOnSelect() {
      let selectionRows = this.$refs.produceUseMx.$refs.grid.getSelectRows();
      if (!selectionRows.length) {
        if (this.messageError) {
          this.messageError.close();
        }
        this.messageError = this.$message.error("请至少选择一个商品");
        return;
      }
      let scztSome = selectionRows.some((item) => item.生产状态 != 1);
      if (scztSome) {
        if (this.messageError) {
          this.messageError.close();
        }
        this.messageError = this.$message.error("请选择生产中单据");
        return;
      }
      // console.log("selectionRows", selectionRows);
      this.$emit("parentCall", ($parent) => {
        let detailRowData = $parent.$refs.detail.rowData;
        let arr = [];
        let stringName = "";
        detailRowData.forEach((item) => {
          selectionRows.forEach((j) => {
            if (j.kid == item.goodsInventoryWarehouseXm && item.Type == 0) {
              stringName += stringName ? "," + j.配件名称 : j.配件名称;
              arr.push(j.kid);
            }
          });
        });
        if (stringName != "") {
          this.$message.warning("明细表已存在 " + stringName);
        }
        arr.forEach((item) => {
          let index = selectionRows.findIndex((x) => x.kid == item);
          if (index != -1) {
            selectionRows.splice(index, 1);
          }
        });
        let newArr = selectionRows.map((item) => {
          return {
            Produce_use: item.ID,
            goodsInventoryWarehouseXm: item.kid,
            Goods_sub: item.配件编码,
            Goods_name: item.配件名称,
            Goods_num: 1,
            Goods_size: item.配件规格,
            WHSE_name: item.配件库位,
            Type: "0",
            AvailableQuantity: item.配件数量,
            Cost_price: item.配件成本金额,
            WHSE_no:item.生产工位,
            Tax_exclusive: item.无税成本,
            Cost_accounting: item.成本核算,
          };
        });
        console.log("newArr", newArr);
        $parent.$refs.detail.rowData.unshift(...newArr);
        this.onModelClose();
        return (this.model = false);
      });
    },
  },
};
</script>
      
      <style>
</style>